package com.woniu.controller;


import com.woniu.entity.CollectAndTest;
import com.woniu.model.Result;
import com.woniu.service.CollectAndTestService;
import com.woniu.service.util.JwtUtil;
import com.woniu.service.util.UserTokenUtil;
import com.woniu.service.vo.TestPageVo;
import com.woniu.service.vo.TestVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author D
 * @since 2023-03-27 08:24:57
 */
@RestController
@RequestMapping("/collectAndTest")
public class CollectAndTestController {
    @Autowired
    private CollectAndTestService collectAndTestService;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;


    /**
     * 获取用户收藏的靶场数据
     *
     * @param request 包含用户信息
     * @param currentPage 当前页数
     * @param size 每页数据大小
     * @return Result<TestPageVo<TestVo>> 结果对象，包含靶场数据列表和分页信息
     */
    @RequestMapping("getCollect")
    public Result getCollect(HttpServletRequest request,Long currentPage,Long size){
        Long userCode = Long.valueOf(UserTokenUtil.getUserAccount(request));
        Result result=new Result<>();
        TestPageVo<TestVo> testPageVo = collectAndTestService.getCollect(userCode, currentPage, size);
        if (testPageVo==null||testPageVo.getPageVo().size()==0){
           result = Result.failResult("没有找到靶场!",null);
        }else {
            result = Result.successResult(testPageVo);
        }
        return result;
    }

    /**
     * 根据用户编码获取收藏或实验的靶场数据
     *
     * @param token 用户Token信息
     * @param collectType 靶场类型，1表示收藏，2表示实验
     * @param currentPage 当前页数
     * @param pageSize 每页数据大小
     * @return Result<TestPageVo<TestVo>> 结果对象，包含靶场数据列表和分页信息
     */
    @ApiOperation("1收藏,2实验")
    @RequestMapping("/getCollectOrTestByUserCode")
    public Result<TestPageVo<TestVo>> getCollectOrTestByUserCode(String token, Integer collectType,
                                                                 Integer currentPage, Integer pageSize) {
        Long userCode = Long.valueOf(JwtUtil.parseValueWithoutException(token, "token"));
        TestPageVo<TestVo> collectOrTestPageVo = collectAndTestService.pageCollectByUserCode(userCode, collectType, currentPage, pageSize);
        return Result.successResult(collectOrTestPageVo);
    }

    /**
     * 用户收藏靶场,已经收藏则取消收藏
     *
     * @param testCode    靶场code
     * @param request     用于获取用户code
     * @return 成功或失败的操作提示信息
     */
    @PostMapping("collect")
    public Result<?> collect( Long testCode,HttpServletRequest request)throws Exception{
        Long userCode = Long.valueOf(UserTokenUtil.getUserAccount(request));
        Result<?> result=new Result<>();
        Boolean updateResult = collectAndTestService.collect(testCode,userCode);
        redisTemplate.delete(redisTemplate.keys(userCode+"*"));
        if (updateResult){
            result=Result.successResult("操作成功");
        }else {
            result=Result.failResult("操作失败");
        }
        return result;
    }
    /**
     * 查询用户是否收藏了指定靶场
     *
     * @param request  用于获取用户code
     * @param testCode 靶场编码
     * @return 已经收藏或没有收藏的提示信息
     */
    @GetMapping("getUserCollect")
    public Result<?> getUserCollect(HttpServletRequest request,Long testCode){
        Long userCode = Long.valueOf(UserTokenUtil.getUserAccount(request));
        Result<?> result=new Result<>();
       CollectAndTest collectAndTest = collectAndTestService.getUserOneCollect(userCode,testCode);
        if (collectAndTest==null || collectAndTest.getCollectType()==2){
            result=Result.failResult("没有收藏了");
        }else {
            result=Result.successResult("已经收藏了");
        }
        return result;
    }
}

